var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');

var app = getApp();
Page({
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function() {

  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  data: {
    loadingHidden: true
  },
  wxLogin: function(e) {
    let that = this;
    this.setData({
      loadingHidden: false
    });
    if (e.detail.userInfo == undefined) {
      app.globalData.hasLogin = false;
      util.showErrorToast('微信登录失败');
      that.setData({
        loadingHidden: true
      });
      return;
    }

    user.checkLogin().catch(() => {

      user.loginByWeixin(e.detail.userInfo).then(res => {
        app.globalData.hasLogin = true;
        app.globalData.memberType = res.data.userInfo.memberType;
        wx.setStorageSync("memberType", res.data.userInfo.memberType);
        // console.log(res);
        that.setData({
          loadingHidden: true
        });
        wx.navigateBack({
          delta: 1
        });
       
        var mobile = res.data.userInfo.mobile;
        // console.log(res);
        // console.log(mobile);
        // console.log(mobile.typeof);
        setTimeout(function() {
          // console.log("---------------500-----------------");
          // console.log(mobile);
          if (mobile === undefined || mobile === "") {
            // console.log(mobile);
            wx.navigateTo({
              url: "/pages/auth/bindMobile/bindMobile"
            });
          }
          wx.switchTab({
            url: '/pages/index/index'
          });
        }, 500)
      }).catch((err) => {
        app.globalData.hasLogin = false;
        that.setData({
          loadingHidden: true
        });
        util.showErrorToast('微信登录失败');
      });
    });
  },
  accountLogin: function() {
    wx.navigateTo({
      url: "/pages/auth/accountLogin/accountLogin"
    });
  },
  getPhoneNumber: function(e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  }
})